package com.clb.commonlibrary.base

import android.content.Context
import android.content.pm.ActivityInfo
import android.content.res.Resources
import android.os.Build
import android.os.Bundle
import android.view.MenuItem
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.Toolbar
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.AdaptScreenUtils
import com.clb.commonlibrary.utils.AppContextWrapper
import com.clb.commonlibrary.utils.LanguageUtil
import me.jessyan.autosize.AutoSizeCompat
import me.leefeng.promptlibrary.PromptDialog

abstract class BaseActivity<T : ViewDataBinding> : AppCompatActivity() {
    abstract val layoutId: Int
    lateinit var binding: T
    private var promptDialog: PromptDialog? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        ARouter.getInstance().inject(this)
        AdaptScreenUtils.adaptWidth(resources, 750)
        try {
            requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
        } catch (e: Exception) {
        }
        binding = DataBindingUtil.setContentView(this, layoutId)
        binding.lifecycleOwner = this
        initViewModel(savedInstanceState)
        initView(savedInstanceState)
        initData(savedInstanceState)
        initClick(savedInstanceState)
    }

    open fun initViewModel(savedInstanceState: Bundle?) {

    }

    abstract fun initView(savedInstanceState: Bundle?)

    abstract fun initData(savedInstanceState: Bundle?)

    abstract fun initClick(savedInstanceState: Bundle?)

    fun showLoading() {
        showLoading(null)
    }

    fun showLoading(loadingStr: String?) {
        if (promptDialog == null) {
            promptDialog = PromptDialog(this)
        }
        if (loadingStr.isNullOrEmpty()) {
            promptDialog?.showLoading("加载中...")
        } else {
            promptDialog?.showLoading(loadingStr)
        }
    }

    fun hideLoading() {
        if (promptDialog != null) {
            promptDialog?.dismiss()
        }
    }

    override fun getResources(): Resources {
        runOnUiThread {
            AutoSizeCompat.autoConvertDensity(super.getResources(), 375F, true)
        }
        return super.getResources()
    }

    @RequiresApi(Build.VERSION_CODES.N)
    override fun attachBaseContext(newBase: Context?) {
        //如果不使用工具类也可以在这里处理好 Locale 传入
        val context = newBase?.let {
            AppContextWrapper.wrap(newBase, LanguageUtil.getLocale(newBase))
        }
        super.attachBaseContext(context)
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        if (item.itemId == android.R.id.home) {
            onBackPressed()
        }
        return super.onOptionsItemSelected(item)
    }

    fun initToolbar(toolbar: Toolbar) {
        setSupportActionBar(toolbar)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        supportActionBar?.setHomeButtonEnabled(true)
    }

    /*override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data)
    }*/
}